package coroutines.coroutine.context

import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.startCoroutine

class CoroutineContextTest {
}

fun main() {
    lateinit var coroutineContext: CoroutineContext
    coroutineContext = CustomCoroutineName("2233")
    coroutineContext = coroutineContext.plus(CustomCoroutineExceptionHandler {
        println("error: ${it.message}")
    })

    suspend {
        println("这是一个协程上下文测试")

        throw Throwable("故意抛出的异常")

        "哈哈哈"
    }.startCoroutine(object : Continuation<String> {
        override val context: CoroutineContext = coroutineContext

        override fun resumeWith(result: Result<String>) {
            println("context name = ${context[CustomCoroutineName]?.name}")

            result.onFailure {
                context[CustomCoroutineExceptionHandler]?.onError(it)
            }
        }
    })
}